package edu.ynu.se.xiecheng.achitectureclass.common;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse.BodyHandlers;

public class text {
    public static void main(String[] args) {
        // 定义URL
        String url = "https://fbox360.com/idserver/core/connect/token";
        String scope="fbox";
        String client_id="68db2e8bda8d47b5b5db7eaf71c7dcdd";
        String client_secret="76dc724c95004acab25482d344dab407";
        String grant_type="client_credentials";

        // 定义请求体（JSON格式）
        String formBody = String.format(
                "scope=%s&client_id=%s&client_secret=%s&grant_type=%s",
                scope, client_id, client_secret, grant_type
        );

        // 创建HttpClient实例
        HttpClient client = HttpClient.newHttpClient();

        // 创建HttpRequest实例
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Content-Type", "application/x-www-form-urlencoded")
                .POST(BodyPublishers.ofString(formBody))
                .build();

        // 发送请求并获取响应
        try {
            HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
            System.out.println("Response status code: " + response.statusCode());
            System.out.println("Response body: " + response.body());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}